<template>
  <div>
    <el-form :inline="true" :model="cForm"  class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select v-model="cForm.category1Id" :disabled="show" @change="getCategory2List(cForm.category1Id)" placeholder="请选择">
          <el-option :label="c1.name" v-for="(c1, index1) in category1List" :key="c1.id" :value="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select  v-model="cForm.category2Id" :disabled="show" @change="getCategory3List(cForm.category2Id)" placeholder="请选择">
          <el-option :label="c2.name" v-for="(c2, index2) in category2List" :key="c2.id" :value="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="三级分类">
        <el-select v-model="cForm.category3Id" :disabled="show" placeholder="请选择" @change="getCategoryId">
          <el-option :label="c3.name" v-for="(c3, index3) in category3List" :key="c3.id" :value="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'category-select',
    props:['show'],
    components: {},
    data() {
      return {
        category1List: [],
        category2List: [],
        category3List: [],
        cForm:{
          category1Id:'',
          category2Id:'',
          category3Id:'',
        }
      };
    },
    mounted() {
      this.getCategory1List()
    },
    methods: {
      async getCategory1List(){
        const res = await this.$api.attr.getCategory1();
        if(res.code === 200){
          this.category1List= res.data
        }
      },
      async getCategory2List(category1Id){
        this.category2List = []
        this.category3List = [],
        this.cForm.category2Id = ''
        this.cForm.category3Id = ''
        const res = await this.$api.attr.getCategory2(category1Id);
        if(res.code === 200){
          this.category2List= res.data
        }
      },
      async getCategory3List(category2Id){
        this.category3List = [],
        this.cForm.category3Id = ''
        const res = await this.$api.attr.getCategory3(category2Id);
        if(res.code === 200){
          this.category3List= res.data
        }
      },
      getCategoryId(){
        this.$emit('getCategoryId', this.cForm)
      }
    },
  }
</script>
<style lang='less' scoped>

</style>
